<?php
/**
 * File uploader class
 * @name FileUploader
 * @version 1.0.0
 * @author Ritesh
 * @copyright Ritesh (mailrkj@gmail.com)
 */
class Custom_Uploader
{
	/**
	 * Constant variables
	 */
	const ERR_INVALID_FILE  = 'Invalid file';
	const ERR_FILE_SIZE		= 'File size is too large';
	const ERR_UNKNOWN		= 'Unknown file upload error';
	const ERR_FILE_EXISTS	= 'File already exist';
	

	/**
	 * Default 100 KB
	 * @var string $_filesize
	 */
	private $_filesize = "1048576";
	
	/**
	 *
	 * @var array $_file_types
	 */
	private $_file_types = array('image/pjpeg','image/jpeg','image/ico','image/gif','image/png','image/x-png');

	/**
	 * detault root folder of class file
	 * @var string $_upload_path
	 */
	private $_upload_path = ".";

	/**
	 * default thumbnail folder
	 * @var string  $_thumbnail_folder
	 */
	private $_thumbnail_folder = 'thumbnails';

	/**
	 * Check to create thumbnail
	 * @var boolean $_is_thumbnail
	 */
	private $_is_thumbnail = true;

	/**
	 * width of thumbnail image
	 * @var int $_thumbnail_width
	 */
	private $_thumbnail_width = 80;

	/**
	 * error message
	 * @var string $_error
	 */
	private $_error = '';

	/**
	 * @var string $_uploaded_file
	 */
	private $_uploaded_file = '';

	/**
	 * Add other files
	 * @param array $file_types
	 */
	function addFileTypes($file_types)
	{
		$this->_file_types = array_merge($this->_file_types,$file_types);
	}

	/**
	 * reset upload file types
	 * @param array $file_types
	 */
	function resetFileTypes($file_types)
	{
		$this->_file_types = $file_types;
	}

	/**
	 * set file size
	 * @param int $size
	 */
	function setFileSize($size)
	{
		$this->_filesize = $size;
	}

	/**
	 * set file upload path
	 * @param string $path
	 */
	function setUploadPath($path)
	{
		$this->_upload_path = $path;
	}

	/**
	 * set thumbnail folder name
	 * @param string $name
	 */
	function setThumbnailFolder($name)
	{
		$this->_thumbnail_folder = $folder_name ;
	}

	/**
	 * set error message
	 * @param string $error
	 */
	function setError($error)
	{
		$this->_error = $error;
	}

	/**
	 * return error message
	 * @return string
	 */
	function getError()
	{
		return __($this->_error);
	}

	/**
	 * File upload function to upload file
	 * @param string $input_name
	 * @param boolean $check_file_exists
	 * @param string $saved_file_name without extension
	 * @return boolean
	 */
	function upload( $arrFile, $check_file_exists = false, $saved_file_name = null )
	{
    	if($saved_file_name == null)
			$saved_file_name = $arrFile['name'];
		else
			$saved_file_name = $saved_file_name.".".$this->getFileExt($arrFile['name']);

		# Check file type and size
		if (!$this->isValidFile($arrFile))
			return false;

		# Check for unknown error
		if ($arrFile["error"] > 0)
		{
			$this->setError("Error:100 ".self::ERR_UNKNOWN);
			return false;
		}

		# Check file exist
		if ($check_file_exists && file_exists($this->_upload_path . DS . $saved_file_name))
		{
			$this->setError(self::ERR_FILE_EXISTS);
			return false;
		}

//		try {
            echo $arrFile["tmp_name"];die;
			$result = move_uploaded_file($arrFile["tmp_name"],$this->_upload_path . DS . $saved_file_name);
			if($result && $this->_is_thumbnail)
			{
				$pathToThumb = $this->_upload_path. DS . $this->_thumbnail_folder;
				$pathToImage = $this->_upload_path. DS . $saved_file_name;
				$this->createThumbs ($pathToImage, $pathToThumb, $this->_thumbnail_width);
			}
//		}catch (Exception $e)
//		{
//			$this->setError("Error:101 ".self::ERR_UNKNOWN);
//			return false;
//		}

		if (!$result)
		{
			$this->setError("Error:102 ".self::ERR_UNKNOWN);
			$this->_uploaded_file = '';
			return false;
		}
		else
		{
			$this->_uploaded_file = $saved_file_name;
		}

		return $result;
	}

	function getUploadedFile()
	{
		return $this->_uploaded_file;
	}

	/**
	 * get file extention
	 * @param string $file_name
	 */
	private function getFileExt($file_name)
	{
		$filename = strtolower($file_name) ;
		$exts = explode(".", $file_name) ;
		$n = count($exts)-1;
		$exts = $exts[$n];
		return $exts;
	}

	/**
	 * Check file type and size
	 * @param string $input_name name of file input
	 * @return bool
	 */
	public function isValidFile($arrFile)
	{
		$file_type = $arrFile['type'];
		$file_size = $arrFile['size'];

		# Check file type
		if (!in_array($file_type, $this->_file_types))
		{
			$this->setError(self::ERR_INVALID_FILE);
			return false;
		}

		# Check file size
		if ($file_size > $this->_filesize)
		{
			$this->setError(self::ERR_FILE_SIZE);
			return false;
		}

		return true;
	}

	/**
	 * create thumbnails of uploaded images
	 * @param string $pathToImages
	 * @param string $pathToThumbs
	 * @param string $thumbWidth
	 */
	function createThumbs( $pathToImage, $pathToThumb, $thumbWidth )
	{
		if(!is_dir($pathToThumb)) mkdir ($pathToThumb,0777);
		// open the directory
		$fname					= basename( $pathToImage );
		$thumbnail_image_path	= $pathToThumb. DS . $fname;
		// load image and get image size
		list($width, $height,$type) = getimagesize($pathToImage);
	
		// calculate thumbnail size
		$new_width	= $thumbWidth;
		$new_height	= floor( $height * ( $thumbWidth / $width ) );

		switch ($type)
		{
			case 1 :
				$source = imagecreatefromgif($pathToImage);
				break;
			case 2 :
				$source = imagecreatefromjpeg($pathToImage);
				break;
			case 3 :
				$source = imagecreatefrompng($pathToImage);
				break;
			default:
				return;
				break;
		}

		// create a new temporary image
		$tmp_img = imagecreatetruecolor( $new_width, $new_height );

		// copy and resize old image into new image
		imagecopyresized( $tmp_img, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

		// save thumbnail into a file
		if($type==1)
			imagegif($tmp_img,$thumbnail_image_path);
		else if($type==2)
			imagejpeg($tmp_img,$thumbnail_image_path);
		else if($type==3)
			imagepng($tmp_img,$thumbnail_image_path);
	}
}


?>
